import { firstLetterCase } from './common'
/**
 * validateType
 * @param {string} type - 类型
 * @param {any} val - 判断类型的值
 * @returns {boolean} - 传入的值与传入的类型是否匹配
 */
export const validateType = (type, val) => {
  return (
    `[object ${firstLetterCase(type)}]` === Object.prototype.toString.call(val)
  )
}

export const isObject = (val) => validateType('Object', val)
export const isArray = (val) => validateType('Array', val)
export const isNull = (val) => validateType('Null', val)
export const isNumber = (val) => validateType('Number', val)
export const isUndefined = (val) => validateType('Undefined', val)
export const isBoolean = (val) => validateType('Boolean', val)
export const isDate = (val) => validateType('Date', val)
// 是否为空字符串
export const isEmptyString = (val) => {
  return typeof val === 'string' && val.split('').every((item) => item === ' ')
}
/**
 * 函数名称 isTel
 * @param {string} val - 匹配的字符串
 * @param {string} type - mobile移动电话，landline座机 all两者皆有
 * @returns {boolearn} - 是否校验成功
 */
export const isTel = (val, type = 'mobile') => {
  const mobileReg = /^1[3-9]{1}\d{9}$/
  const landlineReg = /^0\d{2,3}-\d{7,8}$/
  if (type === 'mobile') {
    return mobileReg.test(val)
  } else if (type === 'landline') {
    return landlineReg.test(val)
  } else if (type === 'all') {
    return mobileReg.test(val) || landlineReg.test(val)
  }
}

export const isEmail = (val) => {
  return /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(val)
}

// 判断是否为空
export const isEmpty = (val) => {
  return val === null || val === '' || val === undefined
}